package com.antdesignbackend.backend.service.impl;

import com.antdesignbackend.backend.common.CalendarDate;
import com.antdesignbackend.backend.common.GoogleAuthenticator;
import com.antdesignbackend.backend.entity.*;
import com.antdesignbackend.backend.entity.entityVo.UserRoleVo;
import com.antdesignbackend.backend.mapper.RolesMapper;
import com.antdesignbackend.backend.mapper.UserRolesMapper;
import com.antdesignbackend.backend.mapper.UsersMapper;
import com.antdesignbackend.backend.service.RoleButtonService;
import com.antdesignbackend.backend.service.RoleMenuService;
import com.antdesignbackend.backend.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UsersMapper, Users> implements UserService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    RolesMapper rolesMapper;
    @Autowired
    UserRolesMapper userRolesMapper;
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    RoleButtonService roleButtonService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void CreateUser(UserRoleVo userRoleVo) {
        //insert users table
        Users user = new Users();
        user.setActivity(userRoleVo.getActivity());
        user.setAliasName(userRoleVo.getAliasName());
        user.setAmount(userRoleVo.getAmount());
        user.setAlipay(userRoleVo.getAlipay());
        user.setBankName(userRoleVo.getBankName());
        user.setBankNo(userRoleVo.getBankNo());
        user.setBankUserName(userRoleVo.getBankUserName());
        user.setEmail(userRoleVo.getEmail());
        user.setKeyName(userRoleVo.getKeyName());
        user.setParentID(userRoleVo.getRoleID());
        Date date = new Date();
        String currentDateTime = CalendarDate.dateToStr(date,"yyyy-MM-dd HH:mm:ss");
        user.setCreateDateTime(currentDateTime);
        //初始密码默认123456，后期个人中心修改
        user.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        user.setPhone(userRoleVo.getPhone());
        user.setSex(userRoleVo.getSex());
        user.setStyle(userRoleVo.getStyle());
        user.setUserName(userRoleVo.getUserName());
        user.setUserType(userRoleVo.getUserType());
        user.setWeixinpay(userRoleVo.getWeixinpay());
        user.setSecret(GoogleAuthenticator.generateSecretKey());
        user.setSecret2(GoogleAuthenticator.generateSecretKey());

        usersMapper.insert(user);
        //insert UserRoles
        UserRoles userRoles = new UserRoles();
        userRoles.setRoleID(userRoleVo.getRoleID());
        userRoles.setUserID(user.getUserID());
        userRolesMapper.insert(userRoles);

        //roleButton
        QueryWrapper<RoleButton> roleButtonQueryWrapper1 = new QueryWrapper<>();
        roleButtonQueryWrapper1.eq("roleID", userRoleVo.getRoleID());
        int count = roleButtonService.count(roleButtonQueryWrapper1);

        if (count < 1) { //如果不存在,存在不处理
            QueryWrapper<RoleButton> roleButtonQueryWrapper = new QueryWrapper<>();
            int currentRoleID = Integer.valueOf(userRoleVo.getCurrentRoleID());
            roleButtonQueryWrapper.eq("roleID", currentRoleID);
            //查询创建者角色下的所有功能权限
            List<RoleButton> roleButtonList = roleButtonService.list(roleButtonQueryWrapper);
            if (roleButtonList.size() > 0) {
                for (int i = 0; i < roleButtonList.size(); i++) {
                    RoleButton newEntity = new RoleButton();
                    newEntity.setButtonID(roleButtonList.get(i).getButtonID());
                    newEntity.setRoleID(userRoleVo.getRoleID());
                    roleButtonService.save(newEntity);
                }
            }
        }
        //roleMenu
        QueryWrapper<RoleMenu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.eq("roleID", userRoleVo.getRoleID());
        List<RoleMenu> list = roleMenuService.list(menuQueryWrapper);
        if (list.size() < 1) {
            //不存在该角色下菜单配置
            //查询当前角色
            QueryWrapper<RoleMenu> menuQueryWrapper1 = new QueryWrapper<>();
            menuQueryWrapper1.eq("roleID", userRoleVo.getCurrentRoleID());
            List<RoleMenu> list1 = roleMenuService.list(menuQueryWrapper1);
            if (list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    RoleMenu newEntity = new RoleMenu();
                    newEntity.setRoleID(userRoleVo.getRoleID());
                    newEntity.setMenuID(list1.get(i).getMenuID());
                    newEntity.setActivity(list1.get(i).getActivity());
                    roleMenuService.save(newEntity);
                }
            }
        }
    }
    @Override
    public void updateData(UpdateWrapper<Users> ew) {
        usersMapper.updateData(ew);
    }

}
